<?php
/**
 * 优惠促销管理
 *
 *
 **/

defined('InShopNC') or exit('Access Invalid!');
class discount_promotionControl extends SystemControl{
    //const EXPORT_SIZE = 5000;
    public function __construct() {
        parent::__construct ();
        //Language::read('goods');
    }
    
    public function indexOp(){
        $model_dis_pro=Model('discount_promotion');
        
        $dis_pro_list=$model_dis_pro->getDiscountPromotionList();
        Tpl::output('dis_pro_list',$dis_pro_list);
        Tpl::showpage('dis_promotion.list');
    }
    
    public function dis_promotion_addOp(){
        if(!empty($_POST)){
            //保存数据
            $data['pro_content']=$_POST['pro_content'];
            $data['pro_type']=$_POST['pro_type'];
            $data['pro_major_id']=$_POST['pro_major_id'];
            $data['pro_title']=$_POST['pro_title'];
            $data['add_time']=TIMESTAMP;
            if($_POST['is_release']==null || empty($_POST['is_release'])){
                $data['is_release']=1;
            }else{
                $data['is_release']=$_POST['is_release'];
            }
            $upload		= new UploadFile();
            if($_FILES['pro_image']['name'] != '' ){
            
                $upload->set('default_dir',DIS_PRO_IMG);
                $result = $upload->upfile('pro_image');
                if (!$result){
                    showMessage($upload->error,'','','error');
                }
                $data['pro_image'] = $upload->file_name;
            
            }
            $model_dis_pro=Model('discount_promotion');
            $result=$model_dis_pro->saveDiscountPromotion($data);
            if($result){
                showMessage("保存成功",'index.php?act=discount_promotion&op=index');
            }
        }else{
            Tpl::showpage('dis_promotion.add');
        }
   
    }
    
    public function dis_promotion_delOp(){
        $condition['pro_id']=$_REQUEST['pro_id'];
        $model_dis_pro=Model('discount_promotion');
        $result=$model_dis_pro->delDiscountPromotion($condition);
        if ($result){
            $data['status']=1;
            $data['msg']="删除成功";
            echo json_encode($data);die;
        }else{
            $data['status']=0;
            $data['msg']="删除失败，请联系开发人员";
            echo json_encode($data);die;
        }
    }
    
    /**
     * 修改模板状态
     */
    public function change_statusOp(){
        $model_dis_pro=Model('discount_promotion');
        if ($_POST['is_release']==1){
            $data['is_release']=0;
        }else if($_POST['is_release']==0){
            $data['is_release']=1;
    
        }
    
        $condition['pro_id']=$_POST['pro_id'];
        //查找是否有已经开启的模板
        $result=$model_dis_pro->updateDiscountPromotion($condition,$data);
    
        if ($result){
            $data['status']=1;
            //$daat['is_active']=$data['is_active'];
        }else{
            $data['status']=0;
        }
        echo json_encode($data);die;
    }
    
    /**
     * 编辑
     */
    public function dis_promotion_edtOp(){
       
        $condition['pro_id']=$_REQUEST['pro_id'];
        $model_dis_pro=Model('discount_promotion');
        $dis_pro_info=$model_dis_pro->getDiscountPromotion($condition);

        Tpl::output('dis_pro_info',$dis_pro_info);
        Tpl::showpage('dis_promotion.edt');
    }
    /**
     * 保存编辑
     */
    public function dis_promotion_save_edtOp(){
        
        $model_dis_pro=Model('discount_promotion');
        $condition['pro_id']=$_POST['pro_id'];
        $update['pro_content']=$_POST['pro_content'];
        $update['pro_type']=$_POST['pro_type'];
        $update['pro_major_id']=$_POST['pro_major_id'];
        $update['pro_title']=$_POST['pro_title'];
        $update['is_release']=$_POST['is_release'];
        $upload		= new UploadFile();
        
        if($_FILES['pro_image']['name'] != '' ){
        
            $upload->set('default_dir',DIS_PRO_IMG);
            $result = $upload->upfile('pro_image');
            if (!$result){
                showMessage($upload->error,'','','error');
            }
            $update['pro_image'] = $upload->file_name;
        }
        $result=$model_dis_pro->updateDiscountPromotion($condition,$update);
        if($result){
            showMessage("保存成功",'index.php?act=discount_promotion&op=index');
        }else{
            showMessage("编辑失败，请联系开发人员");
        }
    }
    
}